package com.octant.testipc;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.api.IWhiteListCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.android.user.bean.WhiteList;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity implements View.OnClickListener {

    private Spinner spn_country_code;
    private EditText edt_account,edt_password;
    private Button btn_login,btn_reset_password;
    private ArrayList<String> codeList = new ArrayList<>();
    private Context mContext;

    private final String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
    private final Pattern regex = Pattern.compile(check);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = LoginActivity.this;
        initViews();
    }

    private void initViews() {
        spn_country_code = findViewById(R.id.spn_country_code);
        edt_account = findViewById(R.id.edt_account);
        edt_password = findViewById(R.id.edt_password);
        btn_login = findViewById(R.id.btn_login);
        btn_reset_password = findViewById(R.id.btn_reset_password);
        TuyaHomeSdk.getUserInstance().getWhiteListWhoCanSendMobileCodeSuccess(new IWhiteListCallback() {
            @Override
            public void onSuccess(WhiteList whiteList) {
                //Toast.makeText(getApplicationContext(), "Whitelist Success:" + whiteList.getCountryCodes(), Toast.LENGTH_SHORT).show();
                String codes = whiteList.getCountryCodes();
                String[] codeArr = codes.split(",");
                codeList.clear();
                for (int i=0; i<codeArr.length; i++){
                    codeList.add(codeArr[i]);
                }
                ArrayAdapter adapter = new ArrayAdapter<String>(mContext,R.layout.support_simple_spinner_dropdown_item,  codeList);
                spn_country_code.setAdapter(adapter);
            }

            @Override
            public void onError(String code, String error) {
                Toast.makeText(getApplicationContext(), "code: " + code + "error:" + error, Toast.LENGTH_SHORT).show();
            }
        });
        btn_login.setOnClickListener(this);
        btn_reset_password.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:{
                String code = (String) spn_country_code.getSelectedItem();
                String account = edt_account.getText().toString();
                String password = edt_password.getText().toString();
                if (code == null || code.isEmpty()){
                    Toast.makeText(mContext, "国家码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (account == null || account.isEmpty()){
                    Toast.makeText(mContext, "邮箱或手机号不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (password == null || password.isEmpty()){
                    Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                ILoginCallback callback = new ILoginCallback() {
                    @Override
                    public void onSuccess(User user) {
                        Toast.makeText(mContext,
                                "Login success",
                                Toast.LENGTH_SHORT).show();

                        startActivity(
                                new Intent(
                                        mContext,
                                        MainActivity.class
                                )
                        );

                        finish();
                    }

                    @Override
                    public void onError(String code, String error) {
                        Toast.makeText(mContext,
                                "code: " + code + "error:" + error,
                                Toast.LENGTH_SHORT).show();
                    }
                };
                Matcher matcher = regex.matcher(account);
                boolean isEmail = matcher.matches();
                if (isEmail){
                    TuyaHomeSdk.getUserInstance().loginWithEmail(code, account, password, callback);
                }else {
                    TuyaHomeSdk.getUserInstance().loginWithPhonePassword(code, account, password, callback);
                }
            }break;
            case R.id.btn_reset_password:{
                Intent intent = new Intent(mContext,ResetPasswordActivity.class);
                startActivity(intent);
            }break;
        }
    }
}
